﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Views
{
    public partial class TimeSheetTBank : BaseWebpage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["LOGIN"] != null)
                {
                    initialData();
                    clearSession();
                    //ddlEndTime.SelectedValue = endtwork;
                    //ddlStartTime.SelectedValue = startwork;
                }
                else
                {
                    Response.Redirect("/AccessDeniedPage.aspx");
                } 
            }
        }

        private void clearSession()
        {
            var list = new List<OTS_TIME_SHEET>();

            Session["timesheet"] = list;
        }

        protected void BtnSaveData_Click(object sender, EventArgs e)
        {
            //var workhrs = isWorkHour();
            //var totalworkhrs = totalhrs();
            try
            {
                if (isHoliday(Convert.ToDateTime(ddlDate.SelectedValue)))
                {
                    var parentObj = new OTS_TIME_SHEET();
                    parentObj.DATE = Convert.ToDateTime(ddlDate.SelectedValue);
                    parentObj.EMPLOYEE_ID = BaseService.LOGIN.EMPLOYEE_ID;
                    parentObj.HOLIDAY_ID = Utility.CheckInt(hdf_holidayID.Value);
                    parentObj.OTS_WORK_DETAIL = getChild(txtholiday.Text);
                    parentObj.Action = ActionEnum.Create;
                    parentObj.CREATED = DateTime.Now;
                    parentObj.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
                    var cmd = new TimeSheet_Service(parentObj);
                    cmd.Add();
                } 
                else
                {
                    var timesheet = (List<OLTS.MODEL.OTS_TIME_SHEET>)Session["timesheet"];
                    foreach (var o in timesheet.Where(x => x.Action == ActionEnum.Create || x.Action == ActionEnum.Update))
                    {
                        if (o.Action == ActionEnum.Create)
                        {
                            var cmd = new TimeSheet_Service(o);
                            cmd.Add();
                        }
                        else if (o.Action == ActionEnum.Update)
                        {
                            var cmd = new TimeSheet_Service(o);
                            cmd.Edit();
                        } 
                    } 
                }
                clearSession();
                ShowSaveSuccessRedirect();
            }
            catch (Exception ex)
            {
                ShowAlert(ex.Message);
            }
        }

        protected void BtnReset_Click(object sender, EventArgs e)
        {
            Session["workdetails"] = null;

        }

        private List<OTS_WORK_DETAIL> getChild(string details)
        {
            var result = new List<OTS_WORK_DETAIL>();
            var obj = new OTS_WORK_DETAIL();
            obj.DETAIL = details;
            obj.AMOUNT_OF_TIME = 0;
            obj.AMOUNT_OF_TIME_OVER_TIME = 0;
            obj.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
            obj.CREATED = DateTime.Now;
            result.Add(obj);

            return result;
        }

        private void newChild(int id, string details)
        {
            var timesheet = (List<OLTS.MODEL.OTS_TIME_SHEET>)Session["timesheet"];

            var childobj = new OTS_WORK_DETAIL();
            var childlst = new List<OTS_WORK_DETAIL>();
            var workhrs = isWorkHour();
            //childobj.DETAIL = ddlLeave.SelectedItem.ToString();
            childobj.AMOUNT_OF_TIME = totalhrs() - workhrs;
            childobj.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
            childobj.CREATED = DateTime.Now;
            childobj.Action = ActionEnum.Create;
            var parent = timesheet.Where(x => x.ID == id).FirstOrDefault();
            if (parent != null)
            {
                var childlist = parent.OTS_WORK_DETAIL;
                childlist.Add(childobj);

                parent.Action = ActionEnum.Update;
                var cmd = new TimeSheet_Service(parent);
                cmd.Edit();
            }
        }

        private int totalhrs()
        {
            DateTime startTime;
            DateTime endTime;
            TimeSpan ts;
            int total = 0;
            startTime = Convert.ToDateTime(startwork); //opening time stamp
            endTime = Convert.ToDateTime(endtwork); //closing time stamp
            ts = endTime - startTime; //calculate timespan
            total = Convert.ToInt32(ts.TotalHours) - 1;
            return total;
        }

        private void initialData()
        {
            var dt = DateTime.Now;
            isHoliday(dt);
            
            BindDate(); 

        } 

        private bool isHoliday(DateTime date)
        {
            var dt = Convert.ToDateTime(date);
            txtholiday.Enabled = false;
            var currentuser = BaseService.LOGIN.EMPLOYEE_ID;
            var site = BaseService.PROJECT.SITE_ID;
            var yr = DateTime.Now.Year.ToString();
            var cmd = new Holiday_Service(yr, Convert.ToInt16(site));
            var result = cmd.GetListHoliday();
            var isholiday = result.Where(x => x.DATE == dt).FirstOrDefault();
            if (isholiday != null)
            {
                txtholiday.Text = "Holiday : " + isholiday.NAME;
                hdf_holidayID.Value = isholiday.ID.ToString();
                return true;
            }
            else
            {
                return false;
            }
        }
         
        [WebMethod]
        public static string isHoliday(string date)
        {
            var dt = Convert.ToDateTime(date);
            var currentuser = BaseService.LOGIN.EMPLOYEE_ID;
            var site = BaseService.PROJECT.SITE_ID;
            var yr = DateTime.Now.Year.ToString();
            var cmd = new Holiday_Service(yr, Convert.ToInt16(site));
            var result = cmd.GetListHoliday();
            var isholiday = result.Where(x => x.DATE == dt).FirstOrDefault();
            if (isholiday != null)
            {
                return isholiday.NAME;
            }
            else
            {
                return "-";
            }
        } 

        private int isWorkHour()
        {
            var hrs = 0;
            var date = Utility.CheckNullableDate(ddlDate.SelectedValue);
            var _currentuser = BaseService.LOGIN.EMPLOYEE_ID;
            var timesheet = (List<OLTS.MODEL.OTS_TIME_SHEET>)Session["timesheet"];
            var obj = timesheet.Where(x => x.DATE == date && x.EMPLOYEE_ID == _currentuser).ToList();
            foreach (var o in obj)
            {
                var childlst = o.OTS_WORK_DETAIL.ToList();
                foreach (var item in childlst)
                {
                    hrs = Convert.ToInt16(hrs + item.AMOUNT_OF_TIME);
                }
            }
            return hrs;
        }

        private void BindDate()
        {
            ddlDate.Items.Clear();

            var year = DateTime.Now.Year;
            var month = DateTime.Now.Month;
            var cal = System.Globalization.CultureInfo.CurrentCulture.Calendar;
            IEnumerable<int> daysInMonth = Enumerable.Range(1, cal.GetDaysInMonth(year, month));

            foreach (int day in daysInMonth)
            {
                DateTime date = new DateTime(year, month, day);

                if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
                {
                    var dt = date.ToString("yyyy-MM-dd");
                    ddlDate.Items.Add(new ListItem(dt, dt));
                }

            }
            var now = DateTime.Now.ToString("yyyy-MM-dd");
            ddlDate.SelectedValue = now;

            //var dateNow = DateTime.Now.ToString("yyyy-MM-dd");
            //var datelast = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            //var datelast1 = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd");
            //var datelast2 = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
            //var datelast3 = DateTime.Now.AddDays(-4).ToString("yyyy-MM-dd");
            //var datelast4 = DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd");
            //var datelast5 = DateTime.Now.AddDays(-6).ToString("yyyy-MM-dd");
            //var datelast6 = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
            //var datelast7 = DateTime.Now.AddDays(-8).ToString("yyyy-MM-dd");
            //var datelast8 = DateTime.Now.AddDays(-9).ToString("yyyy-MM-dd");
            //var datelast9 = DateTime.Now.AddDays(-10).ToString("yyyy-MM-dd");
            //var datelast10 = DateTime.Now.AddDays(-11).ToString("yyyy-MM-dd");
            //ddlDate.Items.Add(new ListItem(dateNow, dateNow));
            //ddlDate.Items.Add(new ListItem(datelast, datelast));
            //ddlDate.Items.Add(new ListItem(datelast1, datelast1));
            //ddlDate.Items.Add(new ListItem(datelast2, datelast2));
            //ddlDate.Items.Add(new ListItem(datelast3, datelast3));
            //ddlDate.Items.Add(new ListItem(datelast4, datelast4));
            //ddlDate.Items.Add(new ListItem(datelast5, datelast5));
            //ddlDate.Items.Add(new ListItem(datelast6, datelast6));
            //ddlDate.Items.Add(new ListItem(datelast7, datelast7));
            //ddlDate.Items.Add(new ListItem(datelast8, datelast8));
            //ddlDate.Items.Add(new ListItem(datelast9, datelast9));
            //ddlDate.Items.Add(new ListItem(datelast10, datelast10));
        }
         
        protected void ddlDate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}